<?php 
class ContentController extends Controller{

	// 首页
	public function index(){

		// // 获得购物车数据
		// $cart = cart::getAllData();
		// // 分配给前端
		// $this->assign('cart',$cart);
		// p($cart);
		// 获得gid
		$gid = Q('get.gid',0,'intval');
		session('url',__ACTION__."/gid/{$gid}");
		// 获得当前商品信息和详情
		$data = M()->join("__f_goods__ g JOIN __f_goods_detail__ gd ON g.gid=gd.f_goods_gid JOIN __f_brand__ b ON g.f_brand_bid=b.bid JOIN __f_category__ c ON g.f_category_cid=c.cid")->where("g.gid={$gid}")->find();

		// 压入打折
		$sale = $data['new_price']/$data['old_price']*10;
		$data['sale'] = substr($sale, 0,3);

		// 把图片分成数组再压入
		$data['small_pic'] = explode(',', $data['small_pic']);
		$data['middle_pic'] = explode(',', $data['middle_pic']);
		$data['big_pic'] = explode(',', $data['big_pic']);

		// 获得当前商品的属性和属性所对应的类型属性
		$attr = M()->join("__f_goods_attr__ ga JOIN __f_type_attr__ ta ON ga.f_type_attr_taid=ta.taid")->where("ga.f_goods_gid={$gid}&&ta.class=1")->all();
		// 循环数组，把颜色和大小压入商品变量中
		foreach ($attr as $key => $value) {
			if ($value['taname']=='大小') {
				$data['size'][$key]['gaid'] = $value['gaid'];
				$data['size'][$key]['gavalue'] = $value['gavalue'];
			}
			if ($value['taname']=='颜色') {
				$data['color'][$key]['gaid'] = $value['gaid'];
				$data['color'][$key]['gavalue'] = $value['gavalue'];
			}
		}
		// 分配给前端
		// p($data);
		$this->assign('data',$data);
		// p($data);
		// 显示模板
		$this->display();
	}

	// 尺码异步
	public function ajax_size(){
		// p(Q('post.'));die;
		// 获得当前商品gid,可以找到当前商品的货品列表
		$gid = Q('post.gid',0,'intval');
		// 组合id,把逗号去掉
		$group =ltrim(Q('post.group'),',');
		// 找到当前商品所对应的货品列表
		$listData = K('Goods_list')->where("f_goods_gid={$gid}")->all();
		// 定义一个变量
		$many =0;
		// 循环货品列表，如果组合id相同就得到他的数量
		foreach ($listData as $k => $v) {
			if ($v['group']==$group) {
				$many = $v['many'];
			}
		}
		// 获得规格所对应的添加金额
		$arr = explode(',', $group);
		$addmoney = 0;
		foreach ($arr as $k => $v) {
			$find = K('Goods_attr')->where("gaid={$v}")->find();
			$addmoney = $addmoney+$find['addmoney'];
		}
		$data = array(
			0=>$many,
			1=>$addmoney
			);
		// 反出库存数量
		$this->ajax($data);
	}









}




















 ?>